package com.eobard.utils;

import java.io.File;

/**
 * @Author: Eobard Gu
 * @Date: 2025-09-30
 * @Description: MNN工具类
 */
public class MnnUtils {
    public static final String MNN_EXT = ".mnn";
    public static final String JSON_EXT = ".json";

    //mac或linux的源码执行命令
    public static final String MNN_VERSION = "/MNNConvert --version";
    private static final String MNN_TO_JSON_STR = "/MNNConvert -f MNN  --modelFile %s --JsonFile %s";
    private static final String JSON_TO_MNN_STR = "/MNNConvert -f JSON --modelFile %s --MNNModel %s";

    //mac或linux的python执行命令
    public static final String MNN_VERSION_PYTHON = MNN_VERSION.replace(File.separator, "");
    private static final String MNN_TO_JSON_PYTHON_STR = MNN_TO_JSON_STR.replace(File.separator, "");
    private static final String JSON_TO_MNN_PYTHON_STR = JSON_TO_MNN_STR.replace(File.separator, "");

    //mac或linux的python默认执行命令
    public static final String MNN_VERSION_PYTHON_DEFAULT = PythonUtils.PYTHON3_EXPLAINER + " -m MNN.tools.mnn --version";
    private static final String MNN_TO_JSON_PYTHON_STR_DEFAULT = PythonUtils.PYTHON3_EXPLAINER + " -m MNN.tools.mnnconvert -f MNN  --modelFile %s --JsonFile %s";
    private static final String JSON_TO_MNN_PYTHON_STR_DEFAULT = PythonUtils.PYTHON3_EXPLAINER + " -m MNN.tools.mnnconvert -f JSON --modelFile %s --MNNModel %s";


    //构建模型文件转json文件的命令:源码方式执行
    public static String mnnToJsonCommand(String modelFile, String jsonFile) {
        return buildCommand(MNN_TO_JSON_STR, modelFile, jsonFile);
    }


    //构建json文件转模型文件的命令:源码方式执行
    public static String jsonToMnnCommand(String jsonFile, String modelFile) {
        return buildCommand(JSON_TO_MNN_STR, jsonFile, modelFile);
    }


    //python方式执行
    public static String mnnToJsonPythonCommand(String modelFile, String jsonFile, boolean isDefault) {
        String defaultCommand = isDefault ? MNN_TO_JSON_PYTHON_STR_DEFAULT : MNN_TO_JSON_PYTHON_STR;
        return buildCommand(defaultCommand, modelFile, jsonFile);
    }

    //python方式执行
    public static String jsonToMnnPythonCommand(String jsonFile, String modelFile, boolean isDefault) {
        String defaultCommand = isDefault ? JSON_TO_MNN_PYTHON_STR_DEFAULT : JSON_TO_MNN_PYTHON_STR;
        return buildCommand(defaultCommand, jsonFile, modelFile);
    }


    private static String buildCommand(String command, String args1, String args2) {
        return String.format(command, args1, args2);
    }

    public static void main(String[] args) throws InterruptedException {
        System.out.println(System.currentTimeMillis());
        Thread.sleep(1);
        System.out.println(System.currentTimeMillis());
        System.out.println(String.valueOf(System.currentTimeMillis()).substring(7));
    }
}
